package web

import (
	"ksd-social-api/commons/base/controller"
	"ksd-social-api/modules/channel/service"
	"ksd-social-api/modules/user/model/context"
)

var channelService = service.ChannelService{}
var channelCodeService = service.ChannelCodeService{}

type ChannelController struct {
	controller.BaseController
}

func (web *ChannelController) FindChannelZones() {
	web.Ok(channelService.FindChannelZones(web.GetSystemId()))
}

func (web *ChannelController) FindChannels() {
	web.Ok(channelService.FindChannels(web.GetSystemId()))
}

func (web *ChannelController) GetChannels() {
	// 1: 获取课程id
	channelId, _ := web.ParseParamUint64("id")
	// 2: 用户id从哪里获取
	userId := web.GetUserId()
	web.Ok(channelService.GetByIdDetail(channelId, userId, web.GetSystemId()))
}

func (web *ChannelController) FindChannelOrders() {
	userCenterContext := context.UserCenterContext{}
	web.BindJSON(&userCenterContext)
	userCenterContext.UserId = web.GetUserId()
	userCenterContext.SystemId = web.GetSystemId()
	p := channelService.FindMyChannelsOrderPage(userCenterContext)
	web.Ok(p)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询下载和code是否存在
 */
func (web *ChannelController) DuihuanChannel() {
	channelId, _ := web.GetUint64("channelId")
	code := web.GetString("code")
	if len(code) == 0 {
		web.FailCodeMsg(999, "请输入兑换码！")
		return
	}
	channelCode := channelCodeService.DuihuanChannel(code, channelId, web.GetUserId(), web.GetSystemId(), web.GetSystemName())
	// 如果还没兑换，就开始兑换，生成下载订单，同时删除订单
	if !channelCode {
		web.FailCodeMsg(999, "该兑换码不存在或者已被使用！")
		return
	}
	web.Ok("success")
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询下载和code是否存在
 */
func (web *ChannelController) DuihuanCoinChannel() {
	channelId, _ := web.GetUint64("channelId")
	channelCode := channelCodeService.DuihuanCoinChannel(channelId, web.GetUserId(), web.GetSystemId(), web.GetSystemName())
	// 如果还没兑换，就开始兑换，生成下载订单，同时删除订单
	if !channelCode {
		web.FailCodeMsg(999, "学习币不足！")
		return
	}
	web.Ok("success")
}
